package class13;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class CardsInLine3 {
    public static int win(int[] arr){
        if (arr == null || arr.length==0)
            return 0;
        int first = f(arr,0,arr.length-1);
        int second = g(arr,0,arr.length-1);
        return Math.max(first,second);
    }

    public static int f(int[] arr,int L,int R){
        if (L == R)
            return arr[L];
        int p1 = arr[L] + g(arr,L+1,R);
        int p2 = arr[R] + g(arr,L,R-1);
        return Math.max(p1,p2);
    }

    public static int g(int[] arr,int L,int R){
        if (L == R)
            return 0;
        int p1 = f(arr,L+1,R);
        int p2 = f(arr,L,R-1);
        return Math.min(p1,p2);
    }
}
